package com.cn.jinl.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cn.jinl.api.*;
import com.cn.jinl.domain.*;
import com.cn.jinl.utils.CommonUtil;
import com.cn.jinl.utils.JsonResponse;
import com.cn.jinl.utils.QueryForm;
import com.cn.jinl.utils.msgSend.HttpClientUtil;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/firstPage")
public class FirstPageController {

    @Autowired
    private IOrderService orderService;

    @Autowired
    private IOnDutyProductService onDutyProductService;

    @Autowired
    ILinePeiService linePeiService;

    @Autowired
    private IOrderBackInfoService orderBackInfoService;

    @Autowired
    private IProductStorageOutService productStorageOutService;

    @Autowired
    private IProcureStorageService procureStorageService;

    @Autowired
    private IOrderOutInfoService orderOutInfoService;

    @Autowired
    private IProcureService procureService;

    @Autowired
    private IChenxingMachineService chenxingMachineService;

    @Autowired
    private IPlanManageService planManageService;

    @Autowired
    private IPhotovoltaicManageService photovoltaicManageService;

    @Autowired
    private IViolationService violationService;

    @Autowired
    private IPersonnelManageService personnelManageService;


    //订单数量
    @ResponseBody
    @RequestMapping("/getOrderTypeCount/{frontToken}")
    public JsonResponse<Map<String, List<Integer>>> getOrderTypeCount(@PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<Map<String, List<Integer>>> response = new JsonResponse<Map<String, List<Integer>>>();
        Map<String, List<Integer>> result = orderService.getOrderTypeCount();
        response.setResult(result);
        response.setSuccess(true);
        return response;
    }

    //原辅料用量
    @ResponseBody
    @RequestMapping("/getFirstPageLinePeis/{frontToken}")
    public JsonResponse<Map<String, List<Double>>> getFirstPageLinePeis(QueryForm queryForm ,@PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<Map<String, List<Double>>> response = new JsonResponse<Map<String, List<Double>>>();
        Map<String, List<Double>> result = linePeiService.getFirstPageLinePeis(queryForm);
        response.setResult(result);
        response.setSuccess(true);
        return response;
    }

    //机台动态
    @ResponseBody
    @RequestMapping("/getOnDutyProductDetail/{frontToken}")
    public JsonResponse<Map<String,Object>> getOnDutyProductDetail(@PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<Map<String,Object>> response = new JsonResponse<>();
        Map<String,Object> result = onDutyProductService.getOnDutyProductDetail();
        response.setResult(result);
        response.setSuccess(true);
        return response;
    }

    //在产任务
    @ResponseBody
    @RequestMapping("/getTodayOnDutyProductTask/{frontToken}")
    public JsonResponse<List<OnDutyProduct>> getTodayOnDutyProductTask(@PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<List<OnDutyProduct>> response = new JsonResponse<>();
        List<OnDutyProduct> result = onDutyProductService.getTodayOnDutyProductTask();
        for (OnDutyProduct onDutyProduct : result){
            BigDecimal bigDecimal = new BigDecimal(onDutyProduct.getTotalNum());
            int totalNum = bigDecimal.intValue();
            onDutyProduct.setTotalNum(String.valueOf(totalNum));
        }
        response.setResult(result);
        response.setSuccess(true);
        return response;
    }


    //仓储任务
    @ResponseBody
    @RequestMapping("/getStorageDetail/{frontToken}")
    public JsonResponse<Map<String,Integer>> getStorageDetail(@PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<Map<String,Integer>> response = new JsonResponse<>();
        int bxOut = productStorageOutService.getProductOutingStorage();
        int tuIn = orderBackInfoService.getOrderBackInfoing();
        int cgIn = procureStorageService.getProcureStorageWaitingCount();
        Map<String,Integer> result = Maps.newHashMap();
        result.put("bxOut",bxOut);
        result.put("tuIn",tuIn);
        result.put("cgIn",cgIn);
        response.setResult(result);
        response.setSuccess(true);
        return response;
    }
    //ccrw 退货入库任务
    @ResponseBody
    @RequestMapping("/getFirstPageOrderUnBackStorageList")
    public JsonResponse<List<OrderBackInfo>> getStorageDetail() throws Exception{
        JsonResponse<List<OrderBackInfo>> response = new JsonResponse<>();
        List<OrderBackInfo> backInfos = orderBackInfoService.getFirstPageOrderUnBackStorageList();
        response.setResult(backInfos);
        response.setSuccess(true);
        return response;
    }



    @RequestMapping("/getForecast")
    @ResponseBody
    public JsonResponse<JSONObject> getForecast(String cityCode) throws Exception {
        JsonResponse<JSONObject> response = new JsonResponse<JSONObject>();
        String result = HttpClientUtil.doGet("http://t.weather.itboy.net/api/weather/city/"+cityCode);
        JSONObject res  = JSON.parseObject(result);
        response.setResult(res);
        response.setSuccess(true);
        return  response;
    }


    //ccrw  发货单号
    @RequestMapping("/getTodayOrderOutInfoList")
    @ResponseBody
    public JsonResponse<List<OrderOutInfo>> getTodayOrderOutInfoList() throws Exception{
        JsonResponse<List<OrderOutInfo>> response = new JsonResponse<>();
        List<OrderOutInfo> orderOutInfos = orderOutInfoService.getTodayOrderOutInfoList();
        response.setResult(orderOutInfos);
        response.setSuccess(true);
        return response;
    }

    //采购
    @RequestMapping("/getProcureTaskingList")
    @ResponseBody
    public JsonResponse<List<Procure>> getProcureTaskingList() throws Exception{
        JsonResponse<List<Procure>> response = new JsonResponse<>();
        List<Procure> procures = procureService.getProcureTaskingList();
        response.setResult(procures);
        response.setSuccess(true);
        return response;
    }

    //scdd 订单计划与在产
    @RequestMapping("/getFirstOnDutyProducts")
    @ResponseBody
    public JsonResponse<List<OnDutyProduct>> getFirstOnDutyProducts() throws Exception{
        JsonResponse<List<OnDutyProduct>> response = new JsonResponse<>();
        List<OnDutyProduct> onDutyProducts = onDutyProductService.getFirstOnDutyProducts();
        response.setResult(onDutyProducts);
        response.setSuccess(true);
        return response;
    }


    @RequestMapping("/getFirstOrderPlanNum")
    @ResponseBody
    public JsonResponse<Map<String,Integer>> getFirstOrderPlanNum() throws Exception{
        JsonResponse<Map<String,Integer>> response = new JsonResponse<>();
        Map<String,Integer> maps = orderService.getFirstOrderPlanNum();
        response.setResult(maps);
        response.setSuccess(true);
        return response;
    }

    //scdd 配伴投料

    @RequestMapping("/getFirstLinePeiList")
    @ResponseBody
    public JsonResponse<List<LinePei>> getFirstLinePeiList() throws Exception{
        JsonResponse<List<LinePei>> response = new JsonResponse<>();
        List<LinePei> linePeis = linePeiService.getFirstLinePeiList();
        response.setResult(linePeis);
        response.setSuccess(true);
        return response;
    }

    @RequestMapping("/getDyDeviceGongXu")
    @ResponseBody
    public JsonResponse<List<GongXuDyVo>> getDyDeviceGongXu() throws Exception{
        JsonResponse<List<GongXuDyVo>> response = new JsonResponse<>();
        List<GongXuDyVo> gongXuDyVos = chenxingMachineService.getDyDeviceGongXu();
        response.setResult(gongXuDyVos);
        response.setSuccess(true);
        return response;
    }

    @RequestMapping("/getOnDutyProductList")
    @ResponseBody
    public JsonResponse<Map<String,List<OnDutyProduct>>> getOnDutyProductList() throws Exception{
        JsonResponse<Map<String,List<OnDutyProduct>>> response = new JsonResponse<>();
        QueryForm queryForm = new QueryForm();
        Map<String, String> dutyMap = CommonUtil.getOnDutyAndWhiteDay();
        String dutyDay = dutyMap.get("dutyDay");
        String whiteDay = dutyMap.get("whiteDay");
        queryForm.setDutyDay(dutyDay);
        queryForm.setWhiteDay(whiteDay);
        Map<String,List<OnDutyProduct>> onDutyProductList = onDutyProductService.getOnDutyProductList(queryForm);
        response.setResult(onDutyProductList);
        response.setSuccess(true);
        return response;
    }

    @RequestMapping("/getOnDutyAndWhiteDay")
    @ResponseBody
    public JsonResponse<Map<String,String>> getOnDutyAndWhiteDay() throws Exception{
        JsonResponse<Map<String,String>> response = new JsonResponse<>();
        Map<String, String> dutyMap = CommonUtil.getOnDutyAndWhiteDay();
        String whiteDay = dutyMap.get("whiteDay");
        whiteDay = StringUtils.equals(whiteDay,"1")?"白班":"夜班";
        dutyMap.put("whiteDay",whiteDay);
        response.setResult(dutyMap);
        response.setSuccess(true);
        return response;
    }

    //szzt 订单与产线机台动态
    @RequestMapping("/getYearFirstPageOrderInfo")
    @ResponseBody
    public JsonResponse<Map<String,Integer>> getYearFirstPageOrderInfo() throws Exception{
        JsonResponse<Map<String,Integer>> response = new JsonResponse<>();
        Map<String,Integer> result = orderService.getYearFirstPageOrderInfo();
        response.setResult(result);
        response.setSuccess(true);
        return response;
    }

    //szzt 产品
    @RequestMapping("/getFirstPlanManageInfo")
    @ResponseBody
    public JsonResponse<Map<String,String>> getFirstPlanManageInfo() throws Exception{
        JsonResponse<Map<String,String>> response = new JsonResponse<>();
        Map<String,String> result = planManageService.getFirstPlanManageInfo();
        response.setResult(result);
        response.setSuccess(true);
        return response;
    }

    //szzt 产品
    @RequestMapping("/getTodayPhotovoltaicManage")
    @ResponseBody
    public JsonResponse<PhotovoltaicManage> getTodayPhotovoltaicManage() throws Exception{
        JsonResponse<PhotovoltaicManage> response = new JsonResponse<>();
        PhotovoltaicManage result = photovoltaicManageService.getTodayPhotovoltaicManage();
        response.setResult(result);
        response.setSuccess(true);
        return response;
    }

    /***
     * @author : Huiyan feng
     * @date   : 2024/4/13
     * @param  : []
     * @return : com.cn.jinl.utils.JsonResponse<java.util.Map<java.lang.String,java.lang.Object>>
     * @description : scdd 看板生产调度左上方违章
     */

    @RequestMapping("/countViolationToday")
    @ResponseBody
    public JsonResponse<Map<String,Object>> bigBordPhotovoltaicData() throws Exception{
        JsonResponse<Map<String,Object>> response = new JsonResponse<>();
        Map<String,Object> map = violationService.countViolationToday();
        response.setResult(map);
        response.setSuccess(true);
        return response;
    }

    /**
     * @author : Huiyan feng
     * @date   : 2024/4/19
     * @param  :
     * @return :
     * @description :  yyzx大屏左下角人事
     */
    @RequestMapping("/renshi")
    @ResponseBody
    public JsonResponse<Map<String,Object>> bigBordRenShi() throws Exception{
        JsonResponse<Map<String,Object>> response = new JsonResponse<>();
        Map<String,Object> map = new HashMap<>();
        //在职
        int zaizhi = personnelManageService.countZaiZhi();
        //本年招录
        int currentYearNumber = personnelManageService.currentYearNumber();
        //本月招录
        int currentMonthNumber = personnelManageService.currentMonthNumber();
        //最新违章人
        List<Violation> list = violationService.currentFiveViolation();

        map.put("zaizhi",zaizhi);
        map.put("currentMonthNumber",currentMonthNumber);
        map.put("currentYearNumber",currentYearNumber);
        map.put("list",list);


        response.setResult(map);
        response.setSuccess(true);
        return response;
    }


    //zzyx
    //获取订单信息
    @RequestMapping("/getFirstPageOrderInfo")
    @ResponseBody
    public JsonResponse<Map<String,Object>> getFirstPageOrderInfo() throws Exception{
        JsonResponse<Map<String,Object>> response = new JsonResponse<>();
        Map<String,Object> map = orderService.getFirstPageOrderInfo();
        response.setResult(map);
        response.setSuccess(true);
        return response;
    }

    @RequestMapping("/getFirstPageOrderInfoList")
    @ResponseBody
    public JsonResponse<List<Order>> getFirstPageOrderInfoList() throws Exception{
        JsonResponse<List<Order>> response = new JsonResponse<>();
        List<Order> list = orderService.getFirstPageOrderInfoList();
        response.setResult(list);
        response.setSuccess(true);
        return response;
    }

    //原辅料
    @RequestMapping("/getFirstPageFuLiaoInfo")
    @ResponseBody
    public JsonResponse<Map<String,Object>> getFirstPageFuLiaoInfo() throws Exception{
        JsonResponse<Map<String,Object>> response = new JsonResponse<>();
        Map<String,Object> map = linePeiService.getFirstPageFuLiaoInfo();
        response.setResult(map);
        response.setSuccess(true);
        return response;
    }

}
